/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manager;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import manager.exceptions.NonexistentEntityException;
import manager.exceptions.PreexistingEntityException;
import persistencia.Evaluador;

/**
 *
 * @author aridrobo
 */
public class EvaluadorJpaController {

    public EvaluadorJpaController() {
        emf = Persistence.createEntityManagerFactory("EasyFastProyectPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Evaluador evaluador) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(evaluador);
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findEvaluador(evaluador.getIdEvaluador()) != null) {
                throw new PreexistingEntityException("Evaluador " + evaluador + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Evaluador evaluador) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            evaluador = em.merge(evaluador);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = evaluador.getIdEvaluador();
                if (findEvaluador(id) == null) {
                    throw new NonexistentEntityException("The evaluador with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Evaluador evaluador;
            try {
                evaluador = em.getReference(Evaluador.class, id);
                evaluador.getIdEvaluador();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The evaluador with id " + id + " no longer exists.", enfe);
            }
            em.remove(evaluador);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Evaluador> findEvaluadorEntities() {
        return findEvaluadorEntities(true, -1, -1);
    }

    public List<Evaluador> findEvaluadorEntities(int maxResults, int firstResult) {
        return findEvaluadorEntities(false, maxResults, firstResult);
    }

    private List<Evaluador> findEvaluadorEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Evaluador.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Evaluador findEvaluador(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Evaluador.class, id);
        } finally {
            em.close();
        }
    }

    public int getEvaluadorCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Evaluador> rt = cq.from(Evaluador.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

    public Evaluador buscarEvaluador(String login, String pass) {

        javax.persistence.Query query = getEntityManager().createQuery("select e from Evaluador e where e.login = :login and e.pass = :pass");
        query.setParameter("login", login);
        query.setParameter("pass", pass);
        Evaluador e = (Evaluador) query.getSingleResult();

        return e;
    }
}
